package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.byxm.wlgw.bean.ShxxBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.query.ShxxQuery;

public class ShxxService {
	ShxxQuery shxxQuery=new ShxxQuery();
	DataBaseAccess dba=null;
	PreparedStatement ps=null;
	/**
	 * 获取收货信息
	 * @param yhxxid
	 * @return
	 */
    public List<ShxxBean> getShxxByYhxxid(int yhxxid){
    	List<ShxxBean> list=new ArrayList<ShxxBean>();
    	try {
			dba=new DataBaseAccess();
			//PreparedStatement ps=dba.getPstmt(shxxQuery.getShxxSql());
			//ps.setInt(1, yhxxid);
			ShxxBean shxxBean1=new ShxxBean();
			shxxBean1.setShxxid(0);
			shxxBean1.setYhxxid(yhxxid);
			ResultSet rs=dba.executeQuery(shxxQuery.getShxxSql(shxxBean1));
			while(rs.next()){
				ShxxBean shxxBean=new ShxxBean();
				shxxBean.setShxxid(rs.getInt("shxxid"));
				shxxBean.setShdz(rs.getString("shdz"));
				shxxBean.setSjhm(rs.getString("sjhm"));
				shxxBean.setYb(rs.getString("yb"));
				shxxBean.setShrxm(rs.getString("shrxm"));
				shxxBean.setYhxxid(rs.getInt("yhxxid"));
				list.add(shxxBean);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    	return list;
    }
    
    /**
     * 获取单条信息
     */
    public ShxxBean getShxxBeanfromDB(int shxxid){
    	ShxxBean shxxBean=new ShxxBean();
       	try {
			dba=new DataBaseAccess();
			//PreparedStatement ps=dba.getPstmt(shxxQuery.getShxxSql());
			//ps.setInt(1, yhxxid);
			ShxxBean shxxBean1=new ShxxBean();
			shxxBean1.setShxxid(shxxid);
			shxxBean1.setYhxxid(0);
			ResultSet rs=dba.executeQuery(shxxQuery.getShxxSql(shxxBean1));
			if(rs.next()){				
				shxxBean.setShxxid(rs.getInt("shxxid"));
				shxxBean.setShdz(rs.getString("shdz"));
				shxxBean.setSjhm(rs.getString("sjhm"));
				shxxBean.setYb(rs.getString("yb"));
				shxxBean.setShrxm(rs.getString("shrxm"));
				shxxBean.setYhxxid(rs.getInt("yhxxid"));
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    	return shxxBean;
    }
    /**
     * 插入收货信息
     * @param shxxBean
     * @return
     * @throws Exception 
     */
    public boolean insertShxx(ShxxBean shxxBean) throws Exception{
    	int isFlag=0;
    	try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(shxxQuery.insertShxxSql());
			ps.setString(1, shxxBean.getShdz());
			ps.setString(2, shxxBean.getSjhm());
			ps.setString(3, shxxBean.getYb());
			ps.setString(4, shxxBean.getShrxm());
			ps.setInt(5, shxxBean.getYhxxid());
			isFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(isFlag>0){
			return true;
		}else{
			return false;
		}
    }
    /**
     * 删除收货信息
     * @param yhxxid
     * @return
     * @throws Exception
     */
    public boolean deleteShxx(int shxxid) throws Exception{
    	int isFlag=0;
    	try {
			dba=new DataBaseAccess();
			isFlag=dba.executeUpdate(shxxQuery.deleteShxxSql(shxxid));
			dba.commit();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(isFlag>0){
			return true;
		}else{
			return false;
		}
    }
    
    /**
     * 修改收货信息
     * @param shxxBean
     * @return
     * @throws Exception
     */
    public boolean updateShxx(ShxxBean shxxBean) throws Exception{
    	int iFlag=0;
    	try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(shxxQuery.updateShxxSql());
			ps.setString(1, shxxBean.getShdz());
			ps.setString(2, shxxBean.getSjhm());
			ps.setString(3, shxxBean.getYb());
			ps.setString(4, shxxBean.getShrxm());
			ps.setInt(5, shxxBean.getShxxid());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(iFlag>0){
			return true;
		}else{
			return false;
		}
    }
}
